package com.mall.server.service.oms;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dubbo.api.OmsOrderApi;
import com.mall.dubbo.api.OmsOrderOperateHistoryApi;
import com.mall.dubbo.api.OmsOrderReturnApplyApi;
import com.mall.model.domain.OmsOrder;
import com.mall.model.domain.OmsOrderReturnApply;
import com.mall.model.dto.OmsOrderReturnApplyDto;
import com.mall.model.dto.OmsUpdateStatusDto;
import com.mall.model.enums.OrderStatus;
import com.mall.model.vo.CommonPage;
import com.mall.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author : xyiaao
 * @createDate : 2022/4/2 20:15
 */
@Service
public class OmsOrderReturnApplyService {

    @DubboReference
    private OmsOrderReturnApplyApi omsOrderReturnApplyApi;


    @Autowired
    private OmsOrderService omsOrderService;

    @DubboReference
    private OmsOrderOperateHistoryApi omsOrderOperateHistoryApi;


    @DubboReference
    private OmsOrderApi omsOrderApi;

    /**
     * 分页查询退货申请
     *
     * @param omsOrderReturnApplyDto
     * @return
     */
    public CommonPage<OmsOrderReturnApply> page(OmsOrderReturnApplyDto omsOrderReturnApplyDto) {

        Page<OmsOrderReturnApply> page = omsOrderReturnApplyApi.pageByOmsOrderReturnApplyDto(omsOrderReturnApplyDto);
        return new CommonPage<>(page);
    }

    public OmsOrderReturnApply getById(Long id) {

        return omsOrderReturnApplyApi.getById(id);
    }

    public void updateStatus(Long id, OmsUpdateStatusDto omsUpdateStatusDto) {

        OmsOrderReturnApply omsOrderReturnApply = omsOrderReturnApplyApi.getById(id);
        BeanUtils.copyProperties(omsUpdateStatusDto, omsOrderReturnApply);

        // 如果同意退款，则添加订单操作记录
        if (omsOrderReturnApply.getStatus() == 1) {
            omsOrderReturnApply.setHandleTime(new Date());
            OmsOrder omsOrder = omsOrderApi.getById(omsOrderReturnApply.getOrderId());
            omsOrderOperateHistoryApi.insertOmsOrderOperateHistory(UserHolder.getId(), omsOrderReturnApply.getOrderId(), omsOrder.getStatus(), "已同意退款");
        }
        // 如果商家确认收货，则将订单关闭
        if (omsOrderReturnApply.getStatus() == 2) {
            omsOrderReturnApply.setReceiveTime(new Date());
            omsOrderService.updateClose(omsOrderReturnApply.getOrderId(), OrderStatus.CLOSED.getNote());
        }
        // 如果拒绝退款，则添加订单操作记录
        if (omsOrderReturnApply.getStatus() == 3) {
            omsOrderReturnApply.setHandleTime(new Date());
            OmsOrder omsOrder = omsOrderApi.getById(omsOrderReturnApply.getOrderId());
            omsOrderOperateHistoryApi.insertOmsOrderOperateHistory(UserHolder.getId(), omsOrderReturnApply.getOrderId(), omsOrder.getStatus(), "已拒绝退款");
        }
        omsOrderReturnApplyApi.updateById(omsOrderReturnApply);
    }

    public void delete(List<Long> ids) {

        omsOrderReturnApplyApi.removeByIds(ids);
    }

}
